package com.ffcs.web.services;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.ffcs.web.model.dto.CityNameResponse;
import com.ffcs.web.utils.HttpSenderUtil;

@Service
public class MapService {

	private Logger logger = Logger.getLogger(MapService.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 调用百度API 获取城市名称
	 * @param la
	 * @param lo
	 * @return
	 */
	public String getCityName(String la, String lo) {
		String json="";
		try {
			json=HttpSenderUtil.send4Get("http://api.map.baidu.com/geocoder?location="+la+","+lo+"&output=json");
		} catch (Exception e) {
			logger.error("调用百度地图API网络异常");
			e.printStackTrace();
		}
		JSONObject jsonObj = JSONObject.fromObject(json);
		if("OK".equals(jsonObj.get("status"))){
			Object result=jsonObj.get("result");
			jsonObj = JSONObject.fromObject(result);
			result=jsonObj.get("addressComponent");
			jsonObj = JSONObject.fromObject(result);
			logger.info("定位城市为"+jsonObj.getString("city"));
			return jsonObj.getString("city");
		}
		return "";
	}

	public List<CityNameResponse> getCityList() {
		String sql="select w.cityName from wechat_city w order by w.cityIndex asc";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<CityNameResponse>(CityNameResponse.class));
	}

	
}
